package com.ht.log;

import com.ht.aems.dbservice.ConnectionFactory;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2008-12-31
 */
public class MPLFactory {
    private static final Logger log = Logger.getLogger(ConnectionFactory.class);
    private static Map<String, String> access = new HashMap<String, String>();
    private static String db_type = DBTypes.DB_ORACLE;

    public static void initialDbType(String type) {
        db_type = type;
    }

    public static MessagePersistLayer createMsgPersist() {
        if (db_type.equals(DBTypes.DB_ORACLE)) {
            return new MPL4Oracle();
        } else if (db_type.equals(DBTypes.DB_MYSQL)) {
            return new MPL4MySql();
        }
        return null;
    }

    public static CommandPersistLayer createCmdPersist() {
        if (db_type.equals(DBTypes.DB_ORACLE)) {
            return new CmdPLOracle();
        } else if (db_type.equals(DBTypes.DB_MYSQL)) {
            return new CmdPLMysql();
        }
        return null;
    }

    private static String getPersistenceLayerType() {
        if (access == null) {
            System.out.println("config is null");
            return null;
        }
        String type = access.get(ConnectionFactory.PROPERTY_DRIVER);
        if (type.contains(DBTypes.DB_MYSQL))
            return MessagePersistLayer.DB_MYSQL;
        else if (type.contains(DBTypes.DB_ORACLE))
            return DBTypes.DB_ORACLE;
        return null;
    }

    public static void setDbAccess(InputStream stream) {
        Properties p = new Properties();
        try {
            p.load(stream);
            Enumeration<?> keys = p.propertyNames();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                access.put(key, p.getProperty(key));
            }
        } catch (IOException e) {
            log.warn("can not read from stream ");
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length <= 0) {
            System.out.println("args is null");
            return;
        }
        String dbType = "";
        for (String para : args) {
            if (para.contains("properties")) {
                String s = MPLFactory.class.getResource(para).getPath();
                System.out.println(s);
                InputStream stream = MPLFactory.class.getResourceAsStream(para);
                InputStream stream1 = MPLFactory.class.getResourceAsStream(para);
                ConnectionFactory.setConfig(stream1);
                setDbAccess(stream);
                dbType = getPersistenceLayerType();
            }
        }

        if (dbType.contains(DBTypes.DB_MYSQL)) {
            MPL4MySql mpl4MySql = new MPL4MySql();
            CmdPLMysql cmdPLMysql = new CmdPLMysql();
            try {
                mpl4MySql.initial();
                cmdPLMysql.initial();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else if (dbType.contains(DBTypes.DB_ORACLE)) {
            MPL4Oracle mpl4Oracle = new MPL4Oracle();
            CmdPLOracle cmdPLOracle = new CmdPLOracle();
            try {
                mpl4Oracle.initial();
                cmdPLOracle.initial();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else
            System.out.println("dba.properties is null");
    }
}
